﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MulticastTV
{
    /// <summary>
    /// EPG settings (details)
    /// </summary>
    public partial class EpgForm : Form
    {
        static string AppPath = System.Windows.Forms.Application.CommonAppDataPath;
        static string path = Application.StartupPath;

        /// <summary>
        /// Constructor
        /// </summary>
        public EpgForm()
        {
            InitializeComponent();            
        }

        private void SiolEpgForm_Load(object sender, EventArgs e)
        {
            textBox1.Text = Properties.Settings.Default.XmltvFile;

            if (Properties.Settings.Default.XmltvSortingNeeded)
                checkBox1.CheckState = CheckState.Checked;
            else
                checkBox1.CheckState = CheckState.Unchecked;
            
            try
            {
                Types.AvailablePlugin selectedPlugin = GlobalP.Plugins.AvailablePlugins.Find(Properties.Settings.Default.EPGPlugin);
                if (selectedPlugin != null)
                {
                    pluginNames.FileName = selectedPlugin.Instance.Name;
                    pluginNames.updateChannels();

                    // update plugin info
                    label16.Text = selectedPlugin.Instance.Author;
                    label17.Text = selectedPlugin.Instance.Version;
                    label18.Text = selectedPlugin.Instance.PluginType;
                    label19.Text = selectedPlugin.Instance.LockedToUserInterfaceLangue;
                    label20.Text = selectedPlugin.Instance.Comment;
               
                    bindingSource1.DataSource = pluginNames.PluginDt;
                    dataGridView1.Columns[0].DataPropertyName = "cname";
                    dataGridView1.Columns[1].DataPropertyName = "wname";
                    dataGridView1.DataSource = bindingSource1;
                } 
            }
            catch { }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (checkBox1.CheckState == CheckState.Checked)
                Properties.Settings.Default.XmltvSortingNeeded = true;
            else
                Properties.Settings.Default.XmltvSortingNeeded = false;
            
            Properties.Settings.Default.XmltvFile = textBox1.Text;
            Properties.Settings.Default.Save();
            this.Close();
        }

        //"atvlist|*.atvlist|All Files|*.*"
        private void button2_Click(object sender, EventArgs e)
        {
            openFileDialog1.CheckFileExists = true;
            openFileDialog1.Filter = "Plugin channel id list (*.txt)|*.txt|XmlTv file (*.xml)|*.xml";
            openFileDialog1.FileName = "";
            openFileDialog1.InitialDirectory = path + @"\parse\";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                 pluginNames.loadFromCustomFile(openFileDialog1.FileName);
                 pluginNames.loadNames(true); //update
                 XmltvParser.xmltvparser.reload();
            }
        }

        private void SiolEpgForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            // after we finished editing we save to xml !!
            pluginNames.savePluginNames(); //save to xml file
        }

        private void pictureBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            openFileDialog1.CheckFileExists = true;
            openFileDialog1.Filter = "Xmltv (*.xml) | *.xml";
            openFileDialog1.FileName = "";
            openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal); 
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text=openFileDialog1.FileName;
                pluginNames.loadFromCustomFile(openFileDialog1.FileName);
                pluginNames.loadNames(true); //update
                XmltvParser.xmltvparser.reload();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Hidden_settings hs = new Hidden_settings();
            hs.Show();
        }
    }
}
